<?php

function multi_fetch($connomains) {
    $errorURLs = array();
    $intOK = $intError = $intWarning = 0;
    $mh = curl_multi_init();
    foreach ($connomains as $i => $url) {
        log::n('push ' . $url);
        $conn[$i] = curl_init($url);
        curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1); //返回信息  
        curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($conn[$i], CURLOPT_TIMEOUT, 15);
        curl_multi_add_handle($mh, $conn[$i]);
    }
// start performing the request
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active and $mrc == CURLM_OK) {
        // wait for network
        if (curl_multi_select($mh) != -1) {
            // pull in any new data, or at least handle timeouts
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
    if ($mrc != CURLM_OK) {
        $intError++;
        log::w("Curl multi read error $mrc");
    }

    foreach ($connomains as $i => $url) {
        if (($err = curl_error($conn[$i])) == '') {
            $res[$i] = curl_multi_getcontent($conn[$i]);
            $intOK++;
        } else {
            $intWarning++;
            $errorURLs[] = $url;
            log::w("Curl error on handle $i: $err");
        }
        curl_multi_remove_handle($mh, $conn[$i]);
        curl_close($conn[$i]);
    }
    curl_multi_close($mh);

    return array('ok' => $intOK, 'error' => $intError, 'warning' => $intWarning, 'res' => $res, 'errorURLs'=>$errorURLs);
}

?>